package com.lishe.lishe.lishe.module.myorder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.shopingcart.bean.CouponBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bin
 */

public class SelectCouponAdapter extends RecyclerView.Adapter<SelectCouponAdapter.ViewHolder> {

    private Context context;
    private ItemClicker mClicker;
    private List<CouponBean> mList;

    public interface ItemClicker {
        void onItemClick(int position);
    }

    public SelectCouponAdapter(Context context, List<CouponBean> list, ItemClicker clicker) {
        this.context = context;
        this.mList = list;
        this.mClicker = clicker;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_selectcoupon, null);
        return new SelectCouponAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.date.setText(mList.get(position).coupon_use_start_date + "-" + mList.get(position).coupon_use_end_date);
        if (mList.get(position).isSelect) {
            holder.select.setImageResource(R.mipmap.coupon_checkbox_selected);
        } else {
            holder.select.setImageResource(R.mipmap.coupon_checkbox_dafault);
        }
        switch (mList.get(position).coupon_type) {
            case "1":
                holder.couponType.setText("平台");
                break;
            case "2":
                holder.couponType.setText("店铺");
                break;
            case "3":
                holder.couponType.setText("企业");
                break;
            case "4":
                holder.couponType.setText("活动");
                break;
        }
        holder.couponName.setText(mList.get(position).coupon_type_detail);
        holder.condition.setText(mList.get(position).coupon_rule_detail);
        switch (mList.get(position).coupon_rule) {  //优惠券规则
            //1：满减，2：满折，3：满免邮，4：直减
            case "1":
                holder.pricelayout.setVisibility(View.VISIBLE);
                holder.price.setText(mList.get(position).coupon_decrease);
                holder.discountlayout.setVisibility(View.GONE);
                holder.freightlayout.setVisibility(View.GONE);
                break;
            case "2":
                holder.pricelayout.setVisibility(View.GONE);
                holder.freightlayout.setVisibility(View.GONE);
                holder.discountlayout.setVisibility(View.VISIBLE);
                holder.discount.setText(mList.get(position).coupon_discount);
                break;
            case "3":
                holder.pricelayout.setVisibility(View.GONE);
                holder.discountlayout.setVisibility(View.GONE);
                holder.freightlayout.setVisibility(View.VISIBLE);
                break;
            case "4":
                holder.pricelayout.setVisibility(View.VISIBLE);
                holder.price.setText(mList.get(position).coupon_decrease);
                holder.discountlayout.setVisibility(View.GONE);
                holder.freightlayout.setVisibility(View.GONE);
                break;
        }
        holder.total.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < mList.size(); i++) {
                    mList.get(i).isSelect = false;
                }
                mList.get(position).isSelect = true;
                mClicker.onItemClick(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView price;
        TextView condition;
        TextView couponType;
        TextView couponName;
        TextView date;
        View pricelayout;
        View discountlayout;
        View freightlayout;
        TextView discount;
        View total;
        ImageView select;

        public ViewHolder(View v) {
            super(v);
            price = v.findViewById(R.id.price);
            condition = v.findViewById(R.id.condition);
            couponType = v.findViewById(R.id.couponType);
            couponName = v.findViewById(R.id.couponName);
            date = v.findViewById(R.id.date);
            pricelayout = v.findViewById(R.id.pricelayout);
            discountlayout = v.findViewById(R.id.discountlayout);
            freightlayout = v.findViewById(R.id.freightlayout);
            discount = v.findViewById(R.id.discount);
            total = v.findViewById(R.id.total);
            select = v.findViewById(R.id.select);
        }
    }

}
